﻿using System;
using System.Collections.Generic;

abstract class Shape
{
    public abstract double Area { get; }
    public abstract bool IsValid();
}

class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double Area => IsValid() ? Width * Height : 0;
    public override bool IsValid() => Width > 0 && Height > 0;
}

class Square : Rectangle
{
    public Square(double side) : base(side, side) { }
    public override bool IsValid() => Width > 0;
}

class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public override double Area
    {
        get
        {
            if (!IsValid()) return 0;
            double s = (A + B + C) / 2;
            return Math.Sqrt(s * (s - A) * (s - B) * (s - C)); // 海伦公式
        }
    }

    public override bool IsValid() => A > 0 && B > 0 && C > 0 && (A + B > C) && (A + C > B) && (B + C > A);
}

class ShapeFactory
{
    private static Random rand = new Random();

    public static Shape CreateRandomShape()
    {
        int type = rand.Next(3); // 0: 矩形, 1: 正方形, 2: 三角形
        switch (type)
        {
            case 0: // 生成随机矩形
                return new Rectangle(rand.Next(1, 10), rand.Next(1, 10));
            case 1: // 生成随机正方形
                return new Square(rand.Next(1, 10));
            case 2: // 生成随机三角形（确保合法）
                while (true)
                {
                    double a = rand.Next(1, 10);
                    double b = rand.Next(1, 10);
                    double c = rand.Next(1, 10);
                    Triangle triangle = new Triangle(a, b, c);
                    if (triangle.IsValid()) return triangle;
                }
        }
        return null;
    }
}

class Program
{
    static void Main()
    {
        List<Shape> shapes = new List<Shape>();
        double totalArea = 0;

        for (int i = 0; i < 10; i++)
        {
            Shape shape = ShapeFactory.CreateRandomShape();
            shapes.Add(shape);
            totalArea += shape.Area;
            Console.WriteLine($"形状 {i + 1}: 类型={shape.GetType().Name}, 面积={shape.Area:F2}");
        }

        Console.WriteLine($"总面积: {totalArea:F2}");
    }
}
